package org.jeecg.modules.jxc.purchase.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.jeecg.modules.jxc.purchase.entity.GoodsType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @company: yaorange
 * @author: Mr.Huang
 * @version: 1.0
 * @create: 2021-12-22 09:18:41
 */
public class TreeToolUtils {
    private List<GoodsType> rootList;
    private List<GoodsType> bodyList;

    public TreeToolUtils(List<GoodsType> rootList,List<GoodsType> bodyList){
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public List<GoodsType> getTree(){
        if(bodyList != null && !bodyList.isEmpty()){
            Map<Integer, Integer> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));
            return rootList;
        }
        return null;
    }

    public void getChild(GoodsType goodsType,Map<Integer,Integer> map){
        List<GoodsType> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getGoodsTypeId()))
                .filter(c -> c.getPId().equals(goodsType.getGoodsTypeId()))
                .forEach(c ->{
                    map.put(c.getGoodsTypeId(),c.getPId());
                    getChild(c,map);
                    childList.add(c);
                });
        goodsType.setChildren(childList);
    }

}
